﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace DAO
{
    public class SanPhamDAO :  DataAccess
    {
        public SanPham CreateObj(DataRow row)
        {
            SanPham obj = new SanPham();
            obj.MaSP = Utils.ToStr(row["MaSP"]);
            obj.TenSP = Utils.ToStr(row["TenSP"]);
            obj.CV = Utils.ToInt(row["CV"]);
            obj.DonGia = Utils.ToFloat(row["DonGia"]);
            obj.SoLuongTon = Utils.ToInt(row["SoLuongTon"]);
            obj.MaLoaiSP = Utils.ToStr(row["MaLoaiSP"]);
            obj.ID = Utils.ToStr(row["ID"]);
            return obj;
        }
        public List<SanPham> GetList()
        {
            DataTable tb = GetData("EXEC GetData ?", "@TableNameX", "SanPham", OleDbType.VarChar);
            List<SanPham> LstSanPham = new List<SanPham>();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                LstSanPham.Add(CreateObj(tb.Rows[i]));
            }
            return LstSanPham;
        }
        public bool Insert(SanPham obj)
        {
            try
            {
                obj.ID = Utils.ToStr(InsertData("EXEC InsertSanPham ?,?,?,?,?,?", CommandType.Text,
                    "@MaSP", obj.MaSP, OleDbType.VarChar,
                    "@TenSP", obj.TenSP, OleDbType.VarWChar,
                    "@CV", obj.CV, OleDbType.Integer,
                    "@DonGia", obj.DonGia, OleDbType.Double,
                    "@SoLuongTon", obj.SoLuongTon, OleDbType.Integer,
                    "@MaLoaiSP", obj.MaLoaiSP, OleDbType.VarChar));
                if (obj.ID != null)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        public bool Update(SanPham obj)
        {
            try
            {
                return UpdateDeleteData("EXEC UpdateSanPham ?,?,?,?,?,?,?", CommandType.Text,
                    "@ID", obj.ID, OleDbType.VarChar,
                    "@MaSP", obj.MaSP, OleDbType.VarChar,
                    "@TenSP", obj.TenSP, OleDbType.VarWChar,
                    "@CV", obj.CV, OleDbType.Integer,
                    "@DonGia", obj.DonGia, OleDbType.Double,
                    "@SoLuongTon", obj.SoLuongTon, OleDbType.Integer,
                    "@MaLoaiSP", obj.MaLoaiSP, OleDbType.VarChar);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

        }
        public bool Delete(SanPham obj)
        {
            return UpdateDeleteData("EXEC DeleteSanPham ?", CommandType.Text, "@MaSP", obj.MaSP, OleDbType.VarChar);
        }
    }
}
